package com.wsd.jing.controller.system;

import com.wsd.jing.annotation.ControllerLog;
import com.wsd.jing.dto.UserDTO;
import com.wsd.jing.entity.User;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.IUserService;
import com.wsd.jing.validator.group.InsertGroup;
import com.wsd.jing.validator.group.UpdateGroup;
import com.wsd.jing.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: UserController
 * @description: 用户前端控制器
 * @createDate 2022-11-29 04:06
 * @copyright 2022-11-29 04:06 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "用户")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/user")
public class UserController {

    private final IUserService userService;

    @ApiOperation(value = "新增")
    @PostMapping()
    @ControllerLog(action = "新增用户", message = "新增用户失败")
    // @SaCheckPermission("user:add")
    public CommonResponse insertUser(@Validated(InsertGroup.class) @RequestBody UserDTO userQuery) {
        return CommonResponse.success(userService.insertUser(userQuery), "新增成功");
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    @ControllerLog(action = "修改用户", message = "修改用户失败")
    // @SaCheckPermission("user:update")
    public CommonResponse updateUser(@Validated(UpdateGroup.class) @RequestBody UserDTO userQuery) {
        return CommonResponse.success(userService.updateUser(userQuery), "修改成功");
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/{id}")
    @ControllerLog(action = "删除用户", message = "删除用户失败")
    // @SaCheckPermission("user:delete")
    public CommonResponse deleteUser(@PathVariable("id") Integer id) {
        return CommonResponse.success(userService.deleteUser(id), "删除成功");
    }

    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "用户id逗号分割字符", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("")
    @ControllerLog(action = "批量删除用户", message = "批量删除用户失败")
    // @SaCheckPermission("user:batch:delete")
    public CommonResponse deleteUsers(@RequestParam("ids") String ids) {
        return CommonResponse.success(userService.deleteUsers(ids), "批量删除成功");
    }

    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{id}")
    @ControllerLog(action = "查询用户", message = "查询用户失败")
    // @SaCheckPermission("user:get")
    public CommonResponse getUser(@PathVariable("id") Integer id) {
        return CommonResponse.success(userService.getUser(id), "查询成功");
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    @ControllerLog(action = "查询用户列表", message = "查询用户列表失败")
    // @SaCheckPermission("user:list")
    public CommonResponse listUsers(CommonRequest commonRequest, User user) {
        return CommonResponse.success(userService.listUsers(commonRequest, user), "查询成功");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pages")
    @ControllerLog(action = "分页查询用户", message = "分页查询用户失败")
    // @SaCheckPermission("user:page")
    public CommonResponse listPageUsers(CommonRequest commonRequest, User user) {
        return CommonResponse.success(userService.listPageUsers(commonRequest, user), "查询成功");
    }

    @ApiOperation(value = "查询用户权限信息")
    @GetMapping("/info")
    @ControllerLog(action = "查询用户权限信息", message = "查询用户权限信息失败")
    // @SaCheckPermission("user:info")
    public CommonResponse getUserInfo() {
        UserInfoVO userInfo = userService.getUserInfo();
        return CommonResponse.success(userInfo, "查询成功");
    }

    @ApiOperation(value = "重置密码")
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "path", dataType = "Integer", required = true)
    @PutMapping("/password/{id}")
    @ControllerLog(action = "重置密码", message = "重置密码失败")
    // @SaCheckPermission("user:password")
    public CommonResponse resetPassword(@PathVariable("id") Integer id) {
        return CommonResponse.success(userService.resetPassword(id), "重置成功");
    }
}
